const app = getApp();
Page({
  data: {
    loading: false,
    contact: '',
    content: '',
    wxUserID: ''
  },

  formSubmit: function (e) {
    let content = e.detail.value.opinion;
    let contact = e.detail.value.contact;
    let regPhone = /^1[3578]\d{9}$/;
    let regEmail = /^[a-z\d_\-\.]+@[a-z\d_\-]+\.[a-z\d_\-]+$/i;

    if (content === "") {
      wx.showModal({
        title: '提示',
        content: '反馈内容不能为空!',
      });
      return false;
    }

    if (contact === "") {
      wx.showModal({
        title: '提示',
        content: '手机号或者邮箱不能为空!',
      });
      return false;
    }

    if (contact === "" && content === "") {
      wx.showModal({
        title: '提示',
        content: '反馈内容,手机号或者邮箱不能为空!',
      });
      return false;
    }

    if ((!regPhone.test(contact) && !regEmail.test(contact)) || (regPhone.test(contact) && regEmail.test(contact))) {
      wx.showModal({
        title: '提示',
        content: '您输入的手机号或者邮箱有误!',
      });
      return false;
    } else {
      this.setData({
        loading: true
      });

      const wxUserId = app.globalData.userInfo.wxUserId;
      console.log(wxUserId);
      wx.request({
        url: 'https://199shq.com/api/advice',
        method: 'POST',
        header: {
          'Content-Type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        data: {
          "wxUserId": wxUserId,
          "opinion": content,
          "contact": contact,
        },

        success: function (res) {
          if (res.statusCode === 200) {
            wx.showToast({
              title: '反馈成功',
              icon: 'success',
              duration: 1500,
              success: function () {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1500);
              }
            });
          } else {
            wx.showToast({
              title: '反馈失败，请重试',
              icon: 'error',
              duration: 1500
            });
          }
        },
        fail: function () {
          wx.showToast({
            title: '网络请求失败',
            icon: 'error',
            duration: 1500
          });
        }
      });
    }
  }
});